<template>
  <div class="home-container">
    <!-- navbar导航 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <!-- 头部添加搜索插槽，绑定搜索按钮 -->
      <van-button slot="title" type="info" round size="small" icon="search" class="search-btn" to="/search">搜索</van-button>
    </van-nav-bar>

    <!-- 频道列表tab animated打开切换动画 swipeable 滑动切换-->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab :title="channel.name" v-for="channel in channels" :key="channel.id">
        <!-- 文章内容自定义组件 -->
        <Articlelist :channel="channel" />
      </van-tab>

      <!-- tabs插槽 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn">
        <i class="toutiao toutiao-gengduo" @click="isChennelEditShow = true"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup v-model="isChennelEditShow" close-icon-position="top-left" closeable position="bottom" :style="{ height: '100%' }">
      <ChannelEdit :mychannels="channels" :active="active" @update-active="onUpdateActive" />
    </van-popup>
  </div>
</template>
<script>
import { getUserchannels } from '@/api/user'
import Articlelist from './components/Article-list'
import ChannelEdit from './components/ChannelEdit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  data() {
    return {
      active: 0, // 点击频道名称的索引
      channels: [], // 频道列表
      isChennelEditShow: false // 控制编辑频道弹出层的显示/隐藏
    }
  },
  components: {
    Articlelist,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },

  created() {
    this.loadchannels()
  },

  methods: {
    async loadchannels() {
      try {
        const channels = []
        if (this.user) {
          const { data } = await getUserchannels()
          this.channels = data.data.channels
        } else {
          // 未登录，判断是否有本地的频道列表数据
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            // 有拿来用
            const { data } = await getUserchannels()
            this.channels = data.data.channels
          } else {
            // 没有，请求获取默认频道列表
            const { data } = await getUserchannels()
            this.channels = data.data.channels
          }
        }

        this.channel = channels
      } catch (err) {
        console.log('获取用户频道列表失败')
      }
    },
    // 更新选中频道面板的索引，完成点击我的频道就跳转到响应索引的面板

    onUpdateActive(index, isChennelEditShow = true) {
      // console.log('home', index)
      this.active = index
      this.isChennelEditShow = isChennelEditShow
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 30px;
    // vertical-align: middle;
    line-height: 64px;
    .van-icon {
      font-size: 32px;
    }
  }
  //    /deep/深度修改样式
  /deep/.channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
    }
    .van-tab {
      min-width: 200px;
      // height: 82px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    // tab标签激活的 文字color
    .van-tab--active {
      color: #333;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      margin-bottom: 8px;
    }
    .placeholder {
      width: 49px;
      height: 82px;
      flex-shrink: 0;
    }
    // 汉堡按钮
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      display: flex;
      justify-content: center;
      align-items: center;
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 2px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
